package zekezang.org;

import common.utils.activity.ActivityUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class TabNavigateActivity extends Activity {

	private static WebView webView = null;
	private static String navigateString = null;
	private static String baseURL = "file:///android_asset/";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_navigate_activity);

		initWait();

		webView = (WebView) findViewById(R.id.myNavigateWebView);

		init();
		if (navigateString == null) {
			navigateString = getIntent().getStringExtra("navigateString");// "<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'></head><body>ddsdsds</br><a href='http://www.sina.com.cn/'>dd</a></body></html>";
		}
		webView.loadDataWithBaseURL(baseURL, navigateString, "text/html", "utf-8", null);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

	}

	ProgressDialog progressDialog = null;
	Handler handlerWait = null;

	public void initWait() {
		progressDialog = new ProgressDialog(this);
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setMessage("wait...");
		handlerWait = new Handler(Looper.myLooper()) {
			public void handleMessage(android.os.Message msg) {
				if (msg.what == 0) {
					if (!progressDialog.isShowing())
						progressDialog.show();
				}
				if (msg.what == 1) {
					if (progressDialog.isShowing())
						progressDialog.dismiss();
				}
			};
		};
	}

	private String uri = null;

	private void init() {// 初始化
//		webView.setBackgroundColor(Color.parseColor("#ADCBE7"));
		webView.setClickable(true);
		webView.getSettings().setJavaScriptEnabled(true);// 可用JS

		webView.setScrollBarStyle(0);// 滚动条风格，为0就是不给滚动条留空间，滚动条覆盖在网页上
		webView.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
				if (url.startsWith(baseURL)) {
					uri = url.substring(baseURL.length(), url.length());
				}else{
					uri = url;
				}
				Log.i("zekezang", url);
				Log.i("zekezang", uri);

				ActivityUtils.getInstance().leftIN_rightOUT_Transition(TabNavigateActivity.this,
						TabNavigateActivity.this, WebviewActivity.class, "uri", uri);
				return true;
			}// 重写点击动作,用webview载入

		});
		webView.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {// 载入进度改变而触发
				super.onProgressChanged(view, progress);
				Message message = new Message();
				if (progress < 100) {
					message.what = 0;
					handlerWait.sendMessage(message);
				} else {
					message.what = 1;
					handlerWait.sendMessage(message);
				}
			}
		});
		webView.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				return false;
			}
		});

	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {// 捕捉返回键
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			ActivityUtils.getInstance().back_leftOUT_rightIN_Transition(TabNavigateActivity.this,
					getApplicationContext());
		}
		return true;
	}

}
